package com.example.a14110100420.yuejian.WActivity.personal;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a14110100420.yuejian.R;
import com.example.a14110100420.yuejian.WSerivice.PersonalService;
import com.example.a14110100420.yuejian.Wbase.BaseActivity;
import com.example.a14110100420.yuejian.Wbase.Conast;
import com.example.a14110100420.yuejian.Wbean.personal.Personal;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShezhiActivity extends BaseActivity {
    final String userid = getUserid();
    private TextView username;
    private EditText password;
    private TextView tel;
    private TextView city;
    private Button tijiao;
    private String pas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shezhi);
        getSupportActionBar().hide();
        initView();
        getUserinfo();
        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getEdit();
            }
        });

    }
    private void initView(){
        tijiao = (Button) findViewById(R.id.btn_over);
        username= (TextView) findViewById(R.id.edit_username);
        password = (EditText) findViewById(R.id.edit_password);
        tel = (TextView) findViewById(R.id.edit_tel);
        city = (TextView) findViewById(R.id.edit_address);

    }
    private void getUserinfo(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Conast.base_Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PersonalService service = retrofit.create(PersonalService.class);
        Call<Personal> call = service.getUser(userid);
        call.enqueue(new Callback<Personal>() {
            @Override
            public void onResponse(Call<Personal> call, Response<Personal> response) {
                Personal personal = response.body();
                String user_name = personal.getUser_name();
                String user_tel = personal.getTel();
                String user_city = personal.getCity_name();
                if(!user_name.equals("")) {
                    username.setText(user_name);
                }
                if (!user_tel.equals("")) {
                    tel.setText(user_tel);
                }

                if (!user_city.equals("")) {
                    city.setText(user_city);
                }


            }

            @Override
            public void onFailure(Call<Personal> call, Throwable t) {

            }
        });


    }

    private void getEdit(){
         pas = password.getText().toString();
        if (pas != null && "".equals(pas))
        {
            Toast.makeText(ShezhiActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
        }else {
            editUserinfo();
        }

    }
    private void editUserinfo(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Conast.base_Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PersonalService service = retrofit.create(PersonalService.class);
        Call<Integer> call = service.getUserChangePassword(userid,pas);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() == 1){
                    Toast.makeText(ShezhiActivity.this,"密码修改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(ShezhiActivity.this,"密码修改失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });

    }
}
